define(['common/geometry/geometry2d', 'common/breeze/keyboardinput'],
       function(geometry2d, keyboardinput) {

function KeyboardController() {
  this.vectorInfos_ = {};
  this.buttonKeys_ = {};
};

KeyboardController.init = function(clearInputPass) {
  keyboardinput.init(clearInputPass);
};

KeyboardController.prototype.getVector = function(id) {
  vectorInfo = this.vectorInfos_[id];
  vector = new geometry2d.Vector(0, 0);
  if (keyboardinput.down(vectorInfo.upKey)) vector.y += 1;
  if (keyboardinput.down(vectorInfo.rightKey)) vector.x += 1;
  if (keyboardinput.down(vectorInfo.downKey)) vector.y -= 1;
  if (keyboardinput.down(vectorInfo.leftKey)) vector.x -= 1;
  if (vector.x || vector.y) {
    vector.normalize();
  }
  return vector;
};

KeyboardController.prototype.buttonDown = function(id) {
  return keyboardinput.down(this.buttonKeys_[id]);
};

/**
 * Returns the number of events that occurred in the last frame.
 */
KeyboardController.prototype.lastFrameEventCount = function(id, buttonEvent) {
  buttonKey = this.buttonKeys_[id];
  var events = keyboardinput.lastFrameEvents(buttonKey);
  var count = 0;
  for (var i = 0; i < events.length; i++) {
    if (events[i] == buttonEvent) {
      count++;
    }
  }
  return count;
};

KeyboardController.prototype.mapVector = function(
    id, upKey, rightKey, downKey, leftKey) {
  this.vectorInfos_[id] = {
    upKey: upKey,
    rightKey: rightKey,
    downKey: downKey,
    leftKey: leftKey
  };
};

KeyboardController.prototype.mapButton = function(id, key) {
  this.buttonKeys_[id] = key;
};

return KeyboardController;

});
